#coding=utf-8
def compute_credit_score(params):
    import pandas as pd
    credit=pd.DataFrame([params])
    credit.drop(['gender'],axis=1, inplace=True)
    credit.drop(['age'],axis=1, inplace=True)
    credit.drop(['id_number'],axis=1, inplace=True)
    credit.drop(['education'],axis=1, inplace=True)
    credit.drop(['salary'],axis=1, inplace=True)
    credit.drop(['housing'],axis=1, inplace=True)
    credit.drop(['car'],axis=1, inplace=True)
    credit.drop(['current_address_length'],axis=1, inplace=True)
    credit.drop(['current_job_length'],axis=1, inplace=True)
    credit.drop(['job'],axis=1, inplace=True)
    
    credit.drop(['application_id'],axis=1, inplace=True)  
    credit.drop(['contract_id'],axis=1, inplace=True)
    credit.drop(['client_name'],axis=1, inplace=True)
    credit.drop(['longest_month_overdue_5y'],axis=1, inplace=True)
    
    # credit= credit.convert_objects(convert_numeric=True)
    credit = credit.apply(pd.to_numeric, errors="ignore")
    credit['loan_overdue_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['loan_overdue_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['longest_month_overdue_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['longest_overdue_card_month_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['longest_overdue_card_month_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['overdue_card_account_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['total_month_overdue_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['total_month_overdue_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['total_overdue_card_month_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    credit['total_overdue_card_month_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
    
    credit['collateral'].fillna(0,inplace=True)
    credit['duration'].fillna(18.71,inplace=True)
    credit['debt_amount'].fillna(63594.62,inplace=True)
    credit['interest_rate'].fillna('0.0135',inplace=True)
    credit['marriage'].fillna('0',inplace=True)#谨慎性考虑
    credit['loan_approv_1m'].fillna('1',inplace=True)
    credit['credit_card_1m'].fillna('1',inplace=True)
    credit['post_loan_1m'].fillna('1',inplace=True)
    credit['personal_check_1m'].fillna('1',inplace=True)
    credit['loan_approv_1y'].fillna('10',inplace=True)
    credit['credit_card_1y'].fillna('6',inplace=True)
    credit['post_loan_1y'].fillna('11',inplace=True)
    credit['personal_check_1y'].fillna('7',inplace=True)
    credit['num_otsd_loan'].fillna('6',inplace=True)
    credit['num_unliquidated_agency'].fillna('4',inplace=True)
    credit['amount_otsd_loan'].fillna('445850',inplace=True)
    credit['balance_otsd_loan'].fillna('358749',inplace=True)
    credit['loan_overdue_5y'].fillna('0',inplace=True)
    credit['total_month_overdue_5y'].fillna('1',inplace=True)
    credit['loan_overdue_2y'].fillna('0',inplace=True)
    credit['total_month_overdue_2y'].fillna(0,inplace=True)
    credit['longest_month_overdue_2y'].fillna('0',inplace=True)
    credit['num_uncancelled_account'].fillna('13',inplace=True)
    credit['num_uncancelled_card_agency'].fillna('6',inplace=True)
    credit['total_credit_granted'].fillna('323878',inplace=True)
    credit['total_credit_used'].fillna('118341',inplace=True)
    credit['overdue_card_account_5y'].fillna('2',inplace=True)
    credit['total_overdue_card_month_5y'].fillna('4',inplace=True)
    credit['longest_overdue_card_month_5y'].fillna('1',inplace=True)
    credit['overdue_card_account_2y'].fillna('0',inplace=True)
    credit['total_overdue_card_month_2y'].fillna('0',inplace=True)
    credit['longest_overdue_card_month_2y'].fillna('0',inplace=True)
    
    import pickle
    output = open('whitelist_ext_civ_list.pkl', 'rb')
    rst = pickle.load(output)
    output.close()
    
    import woe.feature_process as fp
    import woe.eval as eval
    for r in rst:
        print(r.var_name)
        credit[r.var_name] = fp.woe_trans(credit[r.var_name],r)
    
    from sklearn.externals import joblib
    clf=model = joblib.load('joblib_model.pkl')
    
    coe=clf.coef_        #特征权值系数，后面转换为打分规则时会用到
    
    y_pred=clf.predict(credit)
    
    import numpy as np
    coe_new=coe.reshape(coe[0].shape[0],1)
    
    #计算各个用户的分数
    import math
    credit['linear_y']=-np.dot(credit,coe_new)
    credit['pd']=prob=credit.linear_y.apply(lambda x: 1/(1+math.exp(x)))

    factor = 20 / np.log(2)   #factor即为B pd=2/3为临界点时odds=2
    offset = 600 - 20 * np.log(2) / np.log(2)  #offset即为A，基准分值为600，odds翻倍分数增加20
    credit['score']=score=offset-factor*np.log(credit['pd']/(1-credit['pd']))
    
    score_dict={'pd':prob,'score':score}
    return score_dict
